package com.deer.wms.system.manage.web;

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.system.manage.dao.ValidatorMapper;
import com.deer.wms.system.manage.model.ValidatorModel;
import com.deer.wms.system.manage.service.ValidatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by guo on 2018/10/24.
 */
@Api(description = "检验重复api接口")
@RestController
@RequestMapping("/validator")
public class ValidatorController {

    @Value("${spring.datasource.druid.url}")
//    @Value("jdbc:mysql://localhost:3306/new_wms?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false")
    private String dataName;


    @Autowired
    private ValidatorMapper validatorMapper;

    @Autowired
    private ValidatorService validatorService;

    @GetMapping()
    @ApiOperation(value = "/key", notes = "key")
    public Result add(String tableName) {
        String keyName = validatorMapper.findKey(tableName);
        return ResultGenerator.genSuccessResult(keyName);
    }

    @GetMapping("/getNextId")
    public Result getNextId(String tableName) {
        Integer nextId = validatorService.getNextId(tableName);
        return ResultGenerator.genSuccessResult(nextId);
    }


    @PostMapping()
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
 //                   @ApiImplicitParam( name = "tableName", value = "表名", paramType = "form", dataType = "String", dataTypeClass = String.class,  required = true ),
//                    @ApiImplicitParam( name = "fieldName", value = "字段名", paramType = "form", dataType = "String", dataTypeClass = String.class,  required = true ),
//                    @ApiImplicitParam( name = "fieldValue", value = "字段值", paramType = "form", dataType = "String", dataTypeClass = String.class,  required = true ),
//                    @ApiImplicitParam( name = "type", value = "0-新增 1-更新", paramType = "form", dataType = "String", dataTypeClass = String.class,  required = true ),
//                    @ApiImplicitParam( name = "idName", value = "id名，可不传，后台自动获取", paramType = "form", dataType = "String", dataTypeClass = String.class,  required = false ),
//                    @ApiImplicitParam( name = "idValue", value = "type为0时不传，type为1是必传，id值", paramType = "form", dataType = "Integer", required = false )

    })

    @ApiOperation(value = "/验证重复", notes = "验证重复")
    public Result add(@RequestBody ValidatorModel validatorModel, @ApiIgnore @User CurrentUser currentUser) {
        Integer type = validatorModel.getType();
        String tableName = validatorModel.getTableName();
        Integer idValue = validatorModel.getIdValue();
        String fieldName = validatorModel.getFieldName();
        String fieldValue = validatorModel.getFieldValue();
        String idName = validatorModel.getIdName();
        String[] equal = fieldName.split("_");
        String nameOrCode = equal[equal.length-1];

//        if(idName==null){
//            idName = validatorMapper.findKey(tableName);
//            validatorModel.setIdName(idName);
//        }
        if(idName == null) {
            String tableSchema = dataName.substring(dataName.lastIndexOf("/")+1,dataName.lastIndexOf("?"));
            idName = validatorMapper.findPrimaryKey(new ValidatorModel(tableName,tableSchema));
            validatorModel.setIdName(idName);
        }

        /***
         * 新增
         */
        if(type==0){
            Integer hasChongfu = validatorMapper.findAddValidator(tableName,fieldName,fieldValue);
            if(hasChongfu>0){

                if(nameOrCode.equals("name")){
                    return ResultGenerator.genSuccessResult(CommonCode.NAME_EXISTS );
                }else if(nameOrCode.equals("code")){
                    return ResultGenerator.genSuccessResult(CommonCode.CODE_EXISTS );
                }else {
                    return ResultGenerator.genSuccessResult(CommonCode.EQUALE_EXISTS );
                }
            }else {
                return ResultGenerator.genSuccessResult(CommonCode.NAME_CODE_NO_EXISTS);
            }
        }

        /***
         * 更新
         */
        else if(type ==1) {
//            Integer hasChongfu = validatorMapper.findUpdateValidator(validatorModel);
//            if(hasChongfu>0){
//                if(nameOrCode.equals("name")){
//                    return ResultGenerator.genSuccessResult(CommonCode.NAME_EXISTS );
//                }else if(nameOrCode.equals("code")){
//                    return ResultGenerator.genSuccessResult(CommonCode.CODE_EXISTS );
//                }else {
//                    return ResultGenerator.genSuccessResult(CommonCode.EQUALE_EXISTS );
//                }
//            }else {
//                return ResultGenerator.genSuccessResult(CommonCode.NAME_CODE_NO_EXISTS);
//            }
        }
//
//        }
        return ResultGenerator.genSuccessResult();
    }
}